﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace EntityComponentSystem.Framework
{
    public class BehaviorList : IEnumerable<Behavior>
    {
        private List<Behavior> behaviors = new List<Behavior>();
        private int entity = 0;

        public int Count { get { return behaviors.Count; } }

        public BehaviorList(int entity)
        {
            this.entity = entity;
        }

        public void Add(GameEngine ge, string behavior)
        {
            var b = ge.GetBehavior(behavior);
            if (b != null)
            {
                behaviors.Add(b);
                b.AttachEntity(ge, entity);
            }
        }

        public void Clear(GameEngine ge)
        {
            for (int i = this.Count - 1; i >= 0; i--)
            {
                behaviors[i].DeattachEntity(ge, entity);
                behaviors.RemoveAt(i);  
            }
        }

        public void Remove(GameEngine ge, string name)
        {
            for (int i = this.Count - 1; i >= 0; i--)
            {
                if (behaviors[i].Name == name)
                {
                    behaviors[i].DeattachEntity(ge, entity);
                    behaviors.RemoveAt(i);
                }
            }
        }

        public IEnumerator<Behavior> GetEnumerator()
        {
            foreach (var b in behaviors)
                yield return b;
        }

        System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
        {
            foreach (var b in behaviors)
                yield return b;
        }

    }
}
